package com.example.score.controller;

import com.example.score.dao.SportsmanRepository;
import com.example.score.entity.Sportsman;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sportsmen")
public class SportsmanController {
    private final SportsmanRepository sportsmanRepository;

    @Autowired
    public SportsmanController(SportsmanRepository sportsmanRepository) {
        this.sportsmanRepository = sportsmanRepository;
    }

    @PostMapping
    public Sportsman addSportsman(@RequestBody Sportsman sportsman) {
        return sportsmanRepository.save(sportsman);
    }

    @GetMapping
    public List<Sportsman> getAllSportsmen() {
        return sportsmanRepository.findAll();
    }

    @GetMapping("/{id}")
    public Sportsman getSportsmanById(@PathVariable String id) {
        return sportsmanRepository.findById(Long.valueOf(id)).orElse(null);
    }

    @PutMapping
    public Sportsman updateSportsman(@RequestBody Sportsman updatedSportsman) {
        return sportsmanRepository.save(updatedSportsman);
    }

    @DeleteMapping("/{id}")
    public void deleteSportsman(@PathVariable String id) {
        sportsmanRepository.deleteById(Long.valueOf(id));
    }


}
